﻿using GroupThreeMESProject.DTO;
using GroupThreeMESProject.Entity;
using GroupThreeMESProject.Result;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace GroupThreeMESProject.Service.ProductionPlan
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class ProductionPlanAdd : ApplicationService
    {
        private readonly ISqlSugarClient _db;

        public ProductionPlanAdd(ISqlSugarClient db)
        {
            _db = db;
        }
        [HttpGet("GetProductionAddList")]
        public ApiResult<List<ViewOrder>> GetOrderDetails(string? EquipmentName)
        {
{
                var query5 = from o in _db.Queryable<MesMaterial>().ToList()
                             join cus in _db.Queryable<MesEquipment>().ToList() on o.EquipmentId equals cus.EquipmentId into equipmentJoin
                             from cus in equipmentJoin.DefaultIfEmpty()
                             join oritem in _db.Queryable<MesWorkorder>().ToList() on o.MaterialId equals oritem.MaterialId into workorderJoin
                             from oritem in workorderJoin.DefaultIfEmpty()
                             select new ViewOrder
                             {
                                 MateriaName = o.MaterialName,
                                 EquipmentName = cus.EquipmentName,
                                 PlannedQuantity = oritem?.plannedQuantity ?? 0, // 防止为 null，默认值为0
                                 EquipmentId = cus.EquipmentId
                             };

               

                // 返回最终结果
                var orderDetails = query5.ToList();

                // 根据 EquipmentId 去重
                var distinctOrderDetails = orderDetails.GroupBy(d => d.EquipmentId)
                                                        .Select(g => g.First()) // 获取每个 EquipmentId 的第一个记录
                                                        .ToList();

                // 获取每个 EquipmentId 对应的 ProcedureName
                var equipmentProcedureNames = _db.Queryable<Mes_ProcedureEquipment>()
                                                  .LeftJoin<Mes_WorkingProcedure>((o, cus) => o.procedure_id == cus.Id)
                                                  .Where(o => distinctOrderDetails.Select(d => d.EquipmentId).Contains(o.EquipmentId))
                                                  .Select((o, cus) => new { o.EquipmentId, cus.Procedure_name }) // 获取 EquipmentId 和对应的 ProcedureName
                                                  .ToList();

                // 将 ProcedureName 替换 EquipmentName
                foreach (var order in distinctOrderDetails)
                {
                    var matchingProcedure = equipmentProcedureNames.FirstOrDefault(ep => ep.EquipmentId == order.EquipmentId);
                    if (matchingProcedure != null)
                    {
                        order.ProcedureName = matchingProcedure.Procedure_name; // 替换为 ProcedureName
                    }
                }
                if (!string.IsNullOrEmpty(EquipmentName))
                {
                    distinctOrderDetails = distinctOrderDetails.Where(d => d.EquipmentName == EquipmentName).ToList();
                }
                return ApiResult<List<ViewOrder>>.Success(distinctOrderDetails);
            }

        }
        [HttpPost("FindOrderDetailAdd")]
        public void FindOrderDetailAdd()
        {

        }


    }
}
